<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="apple" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="apple" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="bjAdd(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0 " @click.native="delAdd(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog
      :title="isEmit ? '编辑':'添加'"
      :visible.sync="addVisible"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-oped v-if="addVisible" :id="trId" :isemit="isEmit" :orign-list="orignList" @succac="haccuac" @close="hclose" />
    </el-dialog>

    <el-dialog
      :title="isEmit ? '编辑':'添加'"
      :visible.sync="emitVisible"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-oped v-if="emitVisible" :id="trId" :isemit="isEmit" :orign-list="orignList" @succac="haccuac" @close="hclose" />
    </el-dialog>
  </div>
</template>
<script>
import { delMents, getMents } from '@/api/dement'
import { tranListToTreeData } from '@/utils'
import addOped from './deptloag.vue'
export default {
  components: {
    addOped
  },
  data() {
    return {
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞'
      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }]
      list: [],
      addVisible: false,
      emitVisible: false,
      trId: '',
      isEmit: false,
      orignList: []

    }
  },
  created() {
    this.loadMents()
  },
  methods: {
    async  loadMents() {
      const res = await getMents()
      // console.log(res)
      res.data.depts.shift()
      this.orignList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
      this.list = tranListToTreeData(res.data.depts)
    },
    hAdd(id) {
      this.addVisible = true
      this.trId = id
      this.isEmit = false
    },
    bjAdd(id) {
      this.emitVisible = true
      this.trId = id
      this.isEmit = true
    },
    delAdd(id) {
      this.$confirm('确定删除嘛呜呜呜', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delMents(id)
          this.$message.success(res.message)
          this.loadMents()
        } catch (e) {
          this.$message.error(e.message)
        }
      }

      )
        .catch(() => {})
    },
    haccuac() {
      this.addVisible = false
      this.emitVisible = false
      this.loadMents()
    },
    hclose() {
      this.addVisible = false
      this.emitVisible = false
    }
  }
}
</script>
